package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.home.fragment.DeviceFragment;
import com.example.safeassistan.utile.EventBusBean;
import com.example.safeassistan.widget.MyViewPager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class ActivityDeviceManager extends BaseActivity implements View.OnClickListener {
    private MyViewPager viewPager;
    private List<Fragment> fragmentList;
    private List<String> mTitleList; //Tab的标题
    private MainAdapter adapter;
    private TextView tvCommon,tvSpecial;
    private EditText etEdit;
    private TextView line1,line2,tvSearch;
    private String name="";

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityDeviceManager.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvCommon:
                reSetStatus();
                tvCommon.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                line1.setVisibility(View.VISIBLE);
                viewPager.setCurrentItem(0);
                break;
            case R.id.tvSpecial:
                reSetStatus();
                tvSpecial.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                line2.setVisibility(View.VISIBLE);
                viewPager.setCurrentItem(1);
                break;
            case R.id.tvSearch:
                sentBrodcast(name);
                break;
        }
    }

    public void reSetStatus(){
        tvCommon.setTextColor(getResources().getColor(R.color.gray3));
        tvSpecial.setTextColor(getResources().getColor(R.color.gray3));
        line1.setVisibility(View.INVISIBLE);
        line2.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.device_manager;
    }

    @Override
    protected void initView() {
        setTitleText("设备管理");
        viewPager=findViewById(R.id.viewPager);
        tvSpecial=findViewById(R.id.tvSpecial);
        etEdit=findViewById(R.id.etEdit);
        tvSearch=findViewById(R.id.tvSearch);
        tvCommon=findViewById(R.id.tvCommon);
        tvCommon.setOnClickListener(this::onClick);
        tvSearch.setOnClickListener(this::onClick);
        tvSpecial.setOnClickListener(this::onClick);
        line1=findViewById(R.id.line1);
        line2=findViewById(R.id.line2);

        fragmentList=new ArrayList<>();
        fragmentList.add(DeviceFragment.newInstance(1));
        fragmentList.add(DeviceFragment.newInstance(2));
        mTitleList=new ArrayList<>();
        mTitleList.add("主要设备");
        mTitleList.add("特种设备");

        adapter=new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        etEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                name=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void sentBrodcast(String msg){
        EventBusBean eventBus=new EventBusBean();
        eventBus.setCode(101);
        eventBus.setMsg(msg);
        EventBus.getDefault().post(eventBus);
    }


    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}
